<?php

class Application_Model_TipoUsuario extends Zend_Db_Table_Abstract {

    protected $_name = 'tipo_pessoa';
    protected $_primary = 'codigo';
    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new Application_Model_TipoUsuario();
            return self::$instance;
        }
        return self::$instance;
    }

    public function insert($data) {
        if (isset($data['descricao']) && isset($data['dt_cadastro'])){
            parent::insert($data);
        }
    }

    public function update(array $data, $where = '') {
        $where = 'codigo = ' . $data['codigo'];
        parent::update($data, $where);
    }

    public function getById($id) {
        return $this->fetchRow("codigo = " . $id ." and dt_exclusao is null");
    }

    public function getAll(){
        $sql = 'select descricao,codigo from tipo_pessoa where dt_exclusao is null';
        return $this->getAdapter()->query($sql);
    }


    public function lista($page, $qntPagina, $orderName, $order, $descricao, $dtCadastro) {
        $sql = 'select codigo,descricao,dt_cadastro
                from tipo_pessoa
                where dt_exclusao is null ';
       
        if(isset($descricao) && !empty($descricao)){
            $sql.=' and descricao like \''. $descricao. '%\'';
        }
        
        if(isset($dtCadastro) && !empty($dtCadastro)){
            $sql.=' and dt_cadastro = \''. Util_Function_Util::formataData($dtCadastro, 'Y/M/d'). '\'';
        }
        
        if (isset($orderName) && isset($order)) {
            $sql .= ' order by ' . $orderName . ' ' . $order;
        }
        
        $rs = $this->getAdapter()->query($sql)->fetchAll();
        
        $paginator = Zend_Paginator::factory($rs);
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($qntPagina);

        return $paginator;
    }

    public function remove($id){
        $parceria = $this->getById($id)->toArray();
        $where = ' codigo = ' .$id;
        $parceria['dt_exclusao'] = Zend_Date::now()->get('Y/M/d');
        parent::update($parceria, $where);
    }
    
    
}